﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ElingOA6_Admin_DSJCJ.BLL
{
    /// <summary>
    /// 部门BLL
    /// </summary>
    public class HRDept
    {
        /// <summary>
        /// 部门DAL
        /// </summary>
        private readonly DAL.HRDept dal = new DAL.HRDept();
        /// <summary>
        /// 根据公司编号获取数据
        /// <creator>zhongyl</creator>
        /// <date>2013-07-12</date>
        /// </summary>
        /// <param name="entityNo">组织机构编号</param>
        /// <returns>部门信息集合</returns>
        public List<Model.HRDept> GetListByEntityNo(string entityNo)
        {
            DataSet ds = dal.GetListByEntityNo(entityNo);
            return DataTableToList(ds.Tables[0]);
        }

        /// <summary>
        /// 根据公司编号获取外派机构数据
        /// <creator>chenly</creator>
        /// <date>2013-09-27</date>
        /// </summary>
        /// <param name="entityNo">组织机构编号</param>
        /// <returns>部门信息集合</returns>
        public List<Model.HRDept> GetListEntityNo(string entityNo)
        {
            DataSet ds = dal.GetListEntityNo(entityNo);
            return DataTableToList(ds.Tables[0]);
        }
              /// <summary>
        /// 根据公司编号获取数据(不包括派出机构)
        /// <creator>zhongyl</creator>
        /// <date>2013-07-12</date>
        /// </summary>
        /// <param name="entityNo">组织机构编号</param>
        /// <returns>部门信息集合</returns>
        public List<Model.HRDept> GetListBYFIsAgency(string entityNo,string strwhere)
        {
            DataSet ds = dal.GetListBYFIsAgency(entityNo, strwhere);
            return DataTableToList(ds.Tables[0]);
        }

                /// <summary>
        /// 根据公司编号获取数据(不包括派出机构和虚拟部门)
        /// <creator>zhongyl</creator>
        /// <date>2013-07-12</date>
        /// </summary>
        /// <param name="entityNo">组织机构编号</param>
        /// <returns>部门信息集合</returns>
        public List<Model.HRDept> GetListBYFIsAgencyProp(string entityNo, string strwhere)
        {
            DataSet ds = dal.GetListBYFIsAgencyProp(entityNo, strwhere);
            return DataTableToList(ds.Tables[0]);
        }

        /// <summary>
        /// 根据公司编号获取数据 FDeptNo,FDeptName,FParentDeptNo
        /// <creator>zhongyl</creator>
        /// <date>2013-07-12</date>
        /// </summary>
        /// <param name="entityNo">组织机构编号</param>
        /// <returns>部门信息集合</returns>
        public List<Model.HRDept> GetListNoNameByEntityNo(string entityNo)
        {
            DataSet ds = dal.GetListNoNameByEntityNo(entityNo);
            return DataTableToListPart(ds.Tables[0]);
        }

        /// <summary>
        /// 获得数据列表  FDeptNo,FDeptName,FParentDeptNo
        /// <creator>zhongyl</creator>
        /// <date>2013-07-12</date>
        /// </summary>
        /// <param name="dt">需转换数据源</param>
        /// <returns>转换后数据</returns>
        public List<Model.HRDept> DataTableToListPart(DataTable dt)
        {
            List<Model.HRDept> modelList = new List<Model.HRDept>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Model.HRDept model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Model.HRDept();

                    if (dt.Rows[n]["FDeptNo"] != null && dt.Rows[n]["FDeptNo"].ToString() != "")
                    {
                        model.FDeptNo = dt.Rows[n]["FDeptNo"].ToString();
                    }
                    if (dt.Rows[n]["FDeptName"] != null && dt.Rows[n]["FDeptName"].ToString() != "")
                    {
                        model.FDeptName = dt.Rows[n]["FDeptName"].ToString();
                    }
                    if (dt.Rows[n]["FParentDeptNo"] != null && dt.Rows[n]["FParentDeptNo"].ToString() != "")
                    {
                        model.FParentDeptNo = dt.Rows[n]["FParentDeptNo"].ToString();
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }


        /// <summary>
        /// 获取当前部门和所有下级部门的部门编号 返回类型为'1','2','3'
        /// <creator>zhongyl</creator>
        /// <date>2012-11-13</date>
        /// </summary>
        /// <param name="deptNo">部门编号</param>
        /// <param name="entityNo">组织机构编号</param>
        /// <returns>部门编号</returns>
        public string GetSubDeptNo(string deptNo, string entityNo)
        {
            DataSet ds = dal.GetSubDeptNo(deptNo, entityNo);
            string str = string.Empty;
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                str += "'" + row["FDeptNo"].ToString() + "',";
            }
            if (str != string.Empty)
            {
                str = str.TrimEnd(',');
            }
            return str;
        }


        /// <summary>
        /// 是否存在该记录
        /// <creator>zhongyl</creator>
        /// <date>2013-07-12</date>
        /// </summary>
        /// <param name="fEntityNo">组织机构编码</param>
        /// <param name="fDeptNo">部门编号</param>
        /// <returns>是否存在</returns>
        public bool Exists(string fEntityNo, string fDeptNo)
        {
            return dal.Exists(fEntityNo, fDeptNo);
        }

        /// <summary>
        /// 删除部门和部门岗位数据 事务
        /// <creator>zhongyl</creator>
        /// <date>2013-07-12</date>
        /// </summary>
        /// <param name="fEntityNo">组织机构编号</param>
        /// <param name="fDeptNo">部门编号</param>
        public void DeleteDeptAndPosition(string fEntityNo, string fDeptNo)
        {
            dal.deleteDeptAndPosition(fEntityNo, fDeptNo);
        }

        /// <summary>
        /// 保存部门和部门岗位 事务模式
        /// <creator>zhongyl</creator>
        /// <date>2013-07-12</date>>
        /// </summary>
        /// <param name="model">部门信息</param>
        /// <param name="positionList">岗位信息</param>
        public void AddDeptAndPosition(Model.HRDept model, List<Model.HRPosition> positionList)
        {
            dal.AddDeptAndPosition(model, positionList);
        }

        /// <summary>
        ///更新部分字段
        /// <creator>zhongyl</creator>
        /// <date>2013-07-12</date>
        /// </summary>
        /// <param name="model">部门信息</param>
        public void UpdatePart(Model.HRDept model)
        {
            dal.UpdatePart(model);
        }

               /// <summary>
        /// 根据公司编号获取需要计划总结的单位
        /// <creator>zhongyl</creator>
        /// <date>2013-09-27</date>
        /// </summary>
        /// <param name="entityNo">组织机构编号</param>
        /// <returns>部门信息集合</returns>
        public List<Model.HRDept> GetListByFIsReportPlanSummary(string entityNo)
        {
            DataSet ds = dal.GetListByFIsReportPlanSummary(entityNo);
            return DataTableToListPart(ds.Tables[0]);
        }

        /// <summary>
        /// 获取部门名称
        /// <creator>zhongyl</creator>
        /// <date>2013-07-12</date>
        /// </summary>
        /// <param name="fEntityNo">组织机构编号</param>
        /// <param name="fDeptNo">部门编号</param>
        /// <returns>部门名称</returns>
        public string GetDeptName(string fEntityNo, string fDeptNo)
        {
            return dal.GetDeptName(fEntityNo, fDeptNo);
        }


        /// <summary>
        /// 得到一个对象实体
         ///<creator>zhongyl</creator>
        /// <date>2013-07-12</date>
        /// </summary>
        /// <param name="fEntityNo">组织机构编号</param>
        /// <param name="fDeptNo">部门编号</param>
        /// <returns>部门信息</returns>
        public Model.HRDept GetModel(string fEntityNo, string fDeptNo)
        {

            return dal.GetModel(fEntityNo, fDeptNo);
        }

        /// <summary>
        /// 获得数据列表
        ///<creator>zhongyl</creator>
        /// <date>2013-07-12</date>
        /// </summary>
        /// <param name="dt">需转换数据源</param>
        /// <returns>转换后数据</returns>
        public List<Model.HRDept> DataTableToList(DataTable dt)
        {
            List<Model.HRDept> modelList = new List<Model.HRDept>();
            int rowsCount = dt.Rows.Count;  
            if (rowsCount > 0)
            {
                Model.HRDept model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Model.HRDept();
                    if (dt.Columns.Contains("FEntityNo"))
                    {
                        if (dt.Rows[n]["FEntityNo"] != null && dt.Rows[n]["FEntityNo"].ToString() != "")
                        {
                            model.FEntityNo = dt.Rows[n]["FEntityNo"].ToString();
                        }
                    }
                    if (dt.Rows[n]["FDeptNo"] != null && dt.Rows[n]["FDeptNo"].ToString() != "")
                    {
                        model.FDeptNo = dt.Rows[n]["FDeptNo"].ToString();
                    }
                    if (dt.Rows[n]["FDeptName"] != null && dt.Rows[n]["FDeptName"].ToString() != "")
                    {
                        model.FDeptName = dt.Rows[n]["FDeptName"].ToString();
                    }
                    if (dt.Columns.Contains("FIsAgency"))
                    {
                        if (dt.Rows[n]["FIsAgency"] != null && dt.Rows[n]["FIsAgency"].ToString() != "")
                        {
                            model.FIsAgency = dt.Rows[n]["FIsAgency"].ToString();
                        }
                    }
                    if (dt.Columns.Contains("FDeptProp"))
                    {
                        if (dt.Rows[n]["FDeptProp"] != null && dt.Rows[n]["FDeptProp"].ToString() != "")
                        {
                            model.FDeptProp = dt.Rows[n]["FDeptProp"].ToString();
                        }
                    }
                    if (dt.Columns.Contains("FSeq"))
                    {
                        if (dt.Rows[n]["FSeq"] != null && dt.Rows[n]["FSeq"].ToString() != "")
                        {
                            model.FSeq = int.Parse(dt.Rows[n]["FSeq"].ToString());
                        }
                    }
                    if (dt.Columns.Contains("FChargeLeader"))
                    {
                        if (dt.Rows[n]["FChargeLeader"] != null && dt.Rows[n]["FChargeLeader"].ToString() != "")
                        {
                            model.FChargeLeader = dt.Rows[n]["FChargeLeader"].ToString();
                        }
                    }
                    if (dt.Columns.Contains("FParentDeptNo"))
                    {
                        if (dt.Rows[n]["FParentDeptNo"] != null && dt.Rows[n]["FParentDeptNo"].ToString() != "")
                        {
                            model.FParentDeptNo = dt.Rows[n]["FParentDeptNo"].ToString();
                        }
                    }
                    if (dt.Columns.Contains("FIsEval"))
                    {
                        if (dt.Rows[n]["FIsEval"] != null && dt.Rows[n]["FIsEval"].ToString() != "")
                        {
                            model.FIsEval = dt.Rows[n]["FIsEval"].ToString();
                        }
                    }
                    if (dt.Columns.Contains("FUnionScoreType"))
                    {
                        if (dt.Rows[n]["FUnionScoreType"] != null && dt.Rows[n]["FUnionScoreType"].ToString() != "")
                        {
                            model.FUnionScoreType = dt.Rows[n]["FUnionScoreType"].ToString();
                        }
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }


        /// <summary>
        /// 是否为外派机构
        /// </summary>
        ///<creator>chenly</creator>
        /// <date>2013-09-27</date>
        /// <param name="deptNo">部门编号</param>
        /// <returns>是否为外派机构</returns>
        public string IsAgency(string deptNo, string entityNo)
        {
            return dal.IsAgency(deptNo, entityNo);
        }

        /// <summary>
        /// 获取所有的部门 FDeptNo,FDeptName,FParentDeptNo
        /// <creator>sunlj</creator>
        /// <date>2013-10-16</date>
        /// </summary>
        /// <param name="strEntityNo"></param>
        /// <returns></returns>
        public List<Model.HRDept> GetDeptListAll(string strEntityNo)
        {
            DataSet ds = dal.GetDeptListAll(strEntityNo);
            return DataTableToList(ds.Tables[0]);
        }

        /// <summary>
        /// 获取部门 FDeptNo,FDeptName,FParentDeptNo（虚拟部门除外）
        /// <creator>sunlj</creator>
        /// <date>2013-10-17</date>
        /// </summary>
        /// <param name="strEntityNo">单位编号</param>
        /// <param name="decYear">年度</param>
        /// <returns></returns>
        public List<Model.HRDept> getDeptListNoVirtual(string strEntityNo, decimal decYear)
        {
            DataSet ds = dal.getDeptListNoVirtual(strEntityNo, decYear);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 部门排序
        /// <creator>wangl</creator>
        /// <date>2013-08-02</date> 
        /// </summary>
        /// <param name="deptNos">部门编号集合</param>
        /// <param name="entityNo">公司编号</param>
        public void SeqDeptTop(string[] deptNos, string entityNo)
        {
            dal.SeqDeptTop(deptNos, entityNo); 
        }

        /// <summary>
        /// 获取部门 FDeptNo,FDeptName,FParentDeptNo（虚拟部门除外）
        /// <creator>sunlj</creator>
        /// <date>2013-10-17</date>
        /// </summary>
        /// <param name="strEntityNo">单位编号</param>
        /// <param name="decYear">年度</param>
        /// <returns></returns>
        public List<Model.HRDept> getDeptListNoVirtual(string strEntityNo)
        {
            DataSet ds = dal.getDeptListNoVirtual(strEntityNo);
            return DataTableToList(ds.Tables[0]);
        }
        public string GetFShowContent(string entityNO, string FDeptNo)
        {
            return dal.GetFShowContent(entityNO, FDeptNo);
        }
        public void SetFShowContent(string FShowContent, string entityNO, string FDeptNo)
        {
            dal.SetFShowContent(FShowContent, entityNO, FDeptNo);
        }
    }
}
